package org.wu.framework.log.platform.application.impl;

import org.wu.framework.lazy.orm.web.plus.stereotype.LazyApplication;
import org.wu.framework.log.platform.application.RequestLogApplication;
import org.wu.framework.web.response.Result;
import org.wu.framework.log.platform.domain.model.request.log.RequestLog;
import org.wu.framework.log.platform.application.command.request.log.RequestLogRemoveCommand;
import org.wu.framework.log.platform.application.command.request.log.RequestLogStoryCommand;
import org.wu.framework.log.platform.application.command.request.log.RequestLogUpdateCommand;
import org.wu.framework.log.platform.application.command.request.log.RequestLogQueryListCommand;
import org.wu.framework.log.platform.application.command.request.log.RequestLogQueryOneCommand;
import org.wu.framework.log.platform.application.assembler.RequestLogDTOAssembler;
import org.wu.framework.log.platform.application.dto.RequestLogDTO;

import java.util.stream.Collectors;

import jakarta.annotation.Resource;
import org.wu.framework.log.platform.domain.model.request.log.RequestLogRepository;

import java.util.List;

import org.wu.framework.lazy.orm.database.lambda.domain.LazyPage;

/**
 * describe 请求日志
 *
 * @author Jia wei Wu
 * @date 2024/03/25 04:20 下午
 * @see org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyApplicationImpl
 **/
@LazyApplication
public class RequestLogApplicationImpl implements RequestLogApplication {

    @Resource
    RequestLogRepository requestLogRepository;

    /**
     * describe 新增请求日志
     *
     * @param requestLogStoryCommand 新增请求日志
     * @return {@link Result<RequestLog>} 请求日志新增后领域对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLog> story(RequestLogStoryCommand requestLogStoryCommand) {
        RequestLog requestLog = RequestLogDTOAssembler.INSTANCE.toRequestLog(requestLogStoryCommand);
        return requestLogRepository.story(requestLog);
    }

    /**
     * describe 批量新增请求日志
     *
     * @param requestLogStoryCommandList 批量新增请求日志
     * @return {@link Result<List<RequestLog>>} 请求日志新增后领域对象集合
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<List<RequestLog>> batchStory(List<RequestLogStoryCommand> requestLogStoryCommandList) {
        List<RequestLog> requestLogList = requestLogStoryCommandList.stream().map(RequestLogDTOAssembler.INSTANCE::toRequestLog).collect(Collectors.toList());
        return requestLogRepository.batchStory(requestLogList);
    }

    /**
     * describe 更新请求日志
     *
     * @param requestLogUpdateCommand 更新请求日志
     * @return {@link Result<RequestLog>} 请求日志领域对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLog> updateOne(RequestLogUpdateCommand requestLogUpdateCommand) {
        RequestLog requestLog = RequestLogDTOAssembler.INSTANCE.toRequestLog(requestLogUpdateCommand);
        return requestLogRepository.story(requestLog);
    }

    /**
     * describe 查询单个请求日志
     *
     * @param requestLogQueryOneCommand 查询单个请求日志
     * @return {@link Result<RequestLogDTO>} 请求日志DTO对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLogDTO> findOne(RequestLogQueryOneCommand requestLogQueryOneCommand) {
        RequestLog requestLog = RequestLogDTOAssembler.INSTANCE.toRequestLog(requestLogQueryOneCommand);
        return requestLogRepository.findOne(requestLog).convert(RequestLogDTOAssembler.INSTANCE::fromRequestLog);
    }

    /**
     * describe 查询多个请求日志
     *
     * @param requestLogQueryListCommand 查询多个请求日志
     * @return {@link Result<List<RequestLogDTO>>} 请求日志DTO对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<List<RequestLogDTO>> findList(RequestLogQueryListCommand requestLogQueryListCommand) {
        RequestLog requestLog = RequestLogDTOAssembler.INSTANCE.toRequestLog(requestLogQueryListCommand);
        return requestLogRepository.findList(requestLog).convert(requestLogs -> requestLogs.stream().map(RequestLogDTOAssembler.INSTANCE::fromRequestLog).collect(Collectors.toList()));
    }

    /**
     * describe 分页查询多个请求日志
     *
     * @param requestLogQueryListCommand 分页查询多个请求日志
     * @return {@link Result<LazyPage<RequestLogDTO>>} 分页请求日志DTO对象
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<LazyPage<RequestLogDTO>> findPage(int size, int current, RequestLogQueryListCommand requestLogQueryListCommand) {
        RequestLog requestLog = RequestLogDTOAssembler.INSTANCE.toRequestLog(requestLogQueryListCommand);
        return requestLogRepository.findPage(size, current, requestLog).convert(page -> page.convert(RequestLogDTOAssembler.INSTANCE::fromRequestLog));
    }

    /**
     * describe 删除请求日志
     *
     * @param requestLogRemoveCommand 删除请求日志
     * @return {@link Result<RequestLog>} 请求日志
     * @author Jia wei Wu
     * @date 2024/03/25 04:20 下午
     **/

    @Override
    public Result<RequestLog> remove(RequestLogRemoveCommand requestLogRemoveCommand) {
        RequestLog requestLog = RequestLogDTOAssembler.INSTANCE.toRequestLog(requestLogRemoveCommand);
        return requestLogRepository.remove(requestLog);
    }

}